<?php
require_once dirname(__FILE__)."/robot_pdo.php";

$db_msn=new robot_pdo('127.0.0.1','3306','msn','root','');
$db_msn=$db_msn->get_pdo();

function get_user_info($search){
    global $db_msn;
    $search=mysql_escape_string($search);
    $sql="select * from address_book where chinese_name like '%$search%' or english_name like '%$search%' or email like '%$search%'";
    $stmt=$db_msn->prepare($sql);
    $stmt->execute();
    $res=$stmt->fetchAll();
    if($res){
        foreach($res as $v){
            $str.=$v['job_number'].' , '.$v['chinese_name'].' , '.$v['english_name'].' , '.$v['position'].' , '.$v['telephone_ext'].' , '.$v['mobile'].' , '.$v['email']."\r\n";
        }
    }else{
        $str="对不起，没有相关信息";
    }
    return $str;
}

function process_message($message){
    global $t;
    $message = $t->ProcessMessage(trim($message));
    if($message){
        $message=trim($message[1]);
    }else{
        return false;
    }
    $res['full_message']=$message;
    $res['command']=$message[0];
    //$message_arr = explode(' ',$message);
    //$res['code']=substr($message_arr[0],1);
    //$res['message']=trim(substr($message,strlen($message_arr[0])));
    $res['message']=trim(substr($message,1));
    return $res;
}

function on_chat_message($which,$menp,$message){
    global $t;
    $menp_arr = explode(' ',$menp);$email=$menp_arr[1];$nick=$menp_arr[2];
    #echo mb_detect_encoding($nick)."\r\n";
    #echo $nick=iconv('utf-8',"gb2312",$nick)."\r\n";
    $content=process_message($message);

    if($content['command']!='!'){
        if($content['full_message']){
            $texto=get_user_info($content['full_message']);
            $t->SendText($which,$texto);
        }
        return false;
    }else{
        if(is_array($t->mycontacts)){
            $mycontacts=array_unique($t->mycontacts);
            foreach($mycontacts as $v){
                if(strlen($v)<10)continue;
                if($v!=$email)continue;
                #echo $nick."说：\r\n".$content['message']."\r\n";

                $nick=iconv(mb_detect_encoding($nick),"gb2312",$nick);
                $t->MessageToNew($v,iconv("gb2312",'utf-8',$nick).'说：'."\r\n".$content['message']);
            }
        }
    }
//    switch($content['code']){
//        case 'NOF'://notice_online_friends
//            if(is_array($t->mycontacts)){
//                $mycontacts=array_unique($t->mycontacts);
//                foreach($mycontacts as $v){
//                    if(strlen($v)<10)continue;
//                    $t->MessageToNew($v,$content['message']);
//                }
//            }
//        break;
//        case 'DATE':
//            $texto="当前时间".date("Y-m-d H:i:s");
//        break;
//        case 'NICK':
//            if($content['message'])$t->setNick($content['message']);
//        break;
//        case "CONTACT":
//    		$texto="AL LIST:\r\n";
//    		for($i=0;isset($t->mycontacts[$i]);$i++){
//    			$texto.=$t->mycontacts[$i]."\r\n";
//    		}
//    		$texto.="\r\n\r\nFL LIST:\r\n";
//    		for($i=0;isset($t->onlinefriends[$i]);$i++){
//    			$texto.=$t->onlinefriends[$i]."\r\n";
//    		}
//		break;
//		case "ADD":
//    		if(isset($email))$t->addContact($email);
//    	break;
//    	case "DEL":
//			if(isset($email))$t->delContact($email);
//		break;
//    	case 'LOGMY':
//    	    $t->set_logmy($content['message']);
//    	break;
//    	case 'TICKET':
//    	    $texto=get_ticket_title($content['message']);
//    	break;
//    	case 'U':
//    	    $texto=get_user_info($content['message']);
//    	break;
//    }
//    if(isset($texto)&&$texto){
//        $t->SendText($which,$texto);
//    }
}